package cn.bellychang.ood.payroll;

import cn.bellychang.ood.payroll.domain.Employee;
import cn.bellychang.ood.payroll.domain.Paycheck;
import lombok.Builder;
import lombok.extern.slf4j.Slf4j;

import java.util.Date;
import java.util.List;

/**
 * @author ChangLiang
 * @date 2020/5/19
 */
@Builder
@Slf4j
public class PaydayTransaction {

    private Date date;

    private PayrollService payrollService;

    public void execute(){
        List<Employee> employees = payrollService.getAllEmployees();
        employees.forEach(employee -> {
            // 这里就是高层模块的高层策略 非常清晰
            if (employee.isPayDay(date)) {
                // 如果今天是发薪日 那么生成工资条
                Paycheck paycheck = Paycheck.builder().payPeriodStart(employee.getPayPeriodStartDate(date)).payPeriodEnd(date).build();
                log.warn("employee:{}",employee);
                // 根据工资条支付工资
                employee.payDay(paycheck);
                // 保存支付结果
                payrollService.savePayCheck(paycheck);
            }
        });
    }
}
